package com.loiot.baqi.service.job;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.loiot.baqi.dao.ZpCompanyJobDemandKeysDao;
import com.loiot.baqi.dao.ZpPositionTypeKeysDao;
import com.loiot.baqi.pojo.ZpPositionTypeKeys;
import com.loiot.baqi.status.HeartbeatJobType;
import com.loiot.commons.utils.DateUtil;
import com.loiot.commons.utils.StringUtil;

/**
 * 更新标签库
 * @author Administrator
 *
 */
@Service("postionTypeAnalyzeService")
@Scope("prototype")
public class PostionTypeAnalyzeService extends JobTaskDefine {
    private Logger log = LoggerFactory.getLogger(this.getClass());
	@Resource
    ZpCompanyJobDemandKeysDao zpCompanyJobDemandKeysDao;
	@Resource
	ZpPositionTypeKeysDao zpPositionTypeKeysDao;
	@Override
	public void doTask() {
		try {
			updateMarkers();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.exceptionContent=e.getMessage();
		}finally{
			//添加心跳
			this.jobType=(int)HeartbeatJobType.POSITION_TYPE_ANALYZE.getCode();
			this.addHeartbeat();
		}
	}
	
	public void updateMarkers() throws Exception{
		HashMap<String, Object> pMap = new HashMap<String, Object>();
    	pMap.put("qtype","one");
    	pMap.put("endTime",new Date());
    	pMap.put("startTime",DateUtil.addDays(new Date(), -100));
			List<HashMap<String, Object>> list = zpCompanyJobDemandKeysDao.statisticsZpCompanyJobDemandKeysInfo(pMap);
			if(list!=null && list.size()>0){
	    		for(HashMap<String,Object> map : list){
	    			String position_id = String.valueOf(map.get("position_id"));
	    			String keywords = String.valueOf(map.get("keywords"));
	    			String in_persons = String.valueOf(map.get("in_persons"));
	    			String in_times = String.valueOf(map.get("in_times"));
	    			
	    			
	    			pMap.clear();
	    			//查询标签，如果没有就进行添加
	    			List<String> keys = StringUtil.splitToString(keywords,",");
	    			pMap.put("positionId", position_id);
	    			pMap.put("keywords", keys);
	    			List<ZpPositionTypeKeys> keysDB = zpPositionTypeKeysDao.queryZpPositionTypeKeysList(pMap);
	    			int i=0;
	    			if(keys!=null && keys.size()>0){
	    				for(String key : keys){
	    					//String in_persons_arry [] = in_persons.split(",");
	    					//String in_times_array [] = in_times.split(",");
	    					//不存在
	    					if(!this.isExitsForList(keysDB, key)){
	    							ZpPositionTypeKeys keyBean = new ZpPositionTypeKeys();
	    							//keyBean.setInPerson(Long.parseLong(in_persons_arry[i]));
	    							//System.out.println("in_times_array[i]="+in_times_array[i]);
	    							//keyBean.setInTime(DateUtil.toDate(in_times_array[i], DateUtil.DEFAULT_LONG_FORMAT));
	    							keyBean.setPositionId(Long.parseLong(position_id));
	    							keyBean.setKeyword(key);
	    							this.zpPositionTypeKeysDao.addZpPositionTypeKeys(keyBean);
	    					}
	    					
	    					i++;
	    				}
	    			}
	    			
	    		}

			}
	    	
    	
	}
	
	public int getArrayIndex(String keywords,String eqKeyword){
		String[] sValuesStrings = StringUtils.split(keywords, ",");
		for(int i=0;i<sValuesStrings.length;i++){
			if(sValuesStrings[i].equals(eqKeyword)){
				return i;
			}
		}
		return -1;
	}
	
	/**
	 * 集合中判断是否存在
	 * @param list
	 * @param recommended_mobile
	 * @param mapKey
	 * @return
	 */
	public boolean isExitsForList(List<ZpPositionTypeKeys> list,String keyword){
		boolean flag=false;
		if(list!=null && list.size()>0){
			for(ZpPositionTypeKeys keyObj:list){
				if(keyObj.getKeyword().equalsIgnoreCase(keyword)){
					flag=true;
					break;
				}
			}
		}
		return flag;
	}
	
	
}
	


